package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 通过反射获取类中的私有方法，并调用
 * setAccessible(true) //强行打开访问权限
 * setAccessible(false);//关闭访问权限
 * @author XingHaiYang
 * @date 2022/9/16 11:01
 */
public class ReflectDemo6 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class cls = Class.forName("reflect.Person");
        Object obj = cls.newInstance();
        Method method = cls.getDeclaredMethod("hehe");
        method.setAccessible(true);//强行打开访问权限
        method.invoke(obj);
        method.setAccessible(false);//关闭访问权限
    }
}
